package json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import md5.Student;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class FastJsonTest {
    private static final Log log = LogFactory.getLog(FastJsonTest.class);

    @Test
    public void testJavaPOJOToJsonString() {
//        Student  student= new Student();
//        student.setName("张海强");
//        student.setAge(28);
//        Student student2=new Student();
//        student2.setName("李四");
//        student2.setAge(33);
//        student2.setAddress("星雨华府");
//        String jsonString= JSON.toJSONString(student, SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullListAsEmpty);
//        println(jsonString);
//        log.info(jsonString);
//        Student student1=JSON.parseObject(jsonString,Student.class);
//        println(student1);
//        List<Student> list=new ArrayList<>();
//        list.add(student);
//        list.add(student2);
//        String listJsonString=JSON.toJSONString(list);
//        println(listJsonString);
//        List<Student> students=JSON.parseArray(listJsonString,Student.class);
//        for(Student student3:students){
//            println(student3);
//        }
//        List<Student> students1=JSON.parseObject(listJsonString,new TypeReference<List<Student>>(){});
//        for(Student student3:students1){
//            println(student3);
//        }
        List<Map<String, Object>> mapList = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("1", "hahaha");
        map.put("2", "xixixi");
        mapList.add(map);
        Map<String, Object> map1 = new HashMap<>();
        map1.put("a", "zhu");
        map1.put("b", "dog");
        map1.put("c", 3);
        mapList.add(map1);
        mapList.forEach(map2 -> map2.forEach((k, v) -> println(k + "::::" + v)));
        String mapListString = JSON.toJSONString(mapList);
        println(mapListString);
        List<Map<String, Object>> mapList1 = JSON.parseObject(mapListString, new TypeReference<List<Map<String, Object>>>() {
        });
        println(mapList1.get(1).get("c") instanceof Integer);
        mapList1.forEach(map2 -> map2.forEach((k, v) -> println(k + "::::" + v)));
        Objects.toString(new Object());
        mapList.stream().mapToInt(map2 -> Integer.valueOf(map2.get("c").toString())).reduce(0, Integer::sum);
    }

    @Test
    public void testJackson() throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        List<String> list = new ArrayList<>(16);
        list.add("nishuzhume");
        Student student = new Student("张海强", 28, "星雨华府", list);

        String jacksonString = objectMapper.writeValueAsString(student);
        println(jacksonString);
        Student student1 = objectMapper.readValue(jacksonString, Student.class);

        println(student1);
        LocalDate date = LocalDate.of(2017, 11, 23);
        //调整日期到本周日
        LocalDate localDate = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
        System.out.println(localDate);//2017-11-26
        //调整日期到本月最后一天
        LocalDate localDate1 = localDate.with(TemporalAdjusters.lastDayOfMonth());
        System.out.println(localDate1);//2017-11-30
        TemporalAdjuster temporalAdjuster = t -> t.plus(Period.ofDays(12));

    }

    private void println(Object object) {
        System.out.println(object);
    }
}
